package testsUnitaires;

import java.util.ArrayList;

import tools.StringUtils;

import network.KrakenClient;
import network.messages.KrakenClientMsg;
import network.messages.KrakenServerMsg;
import network.ssl.KrakenSSLClient;

public class TestJeton {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// test demande de jeton
		ArrayList<String> msgList = new ArrayList<String>();
		msgList.add(KrakenClientMsg.KRAKEN_CLIENT.makeCommand());
		//arg1 = ip central, arg2 = ip auth, arg3 = ip site cible, arg4 = ip cam cible
		msgList.add(KrakenClientMsg.ASK_FOR_TOKEN.makeCommand(
				" 192.168.2.91"
				+ "  192.168.2.92"
				+ "  192.168.2.41"
				+ "  Cam12"));
		msgList.add(KrakenClientMsg.END_CLIENT.makeCommand());
		
		KrakenClient krakenClient = new KrakenClient(msgList, "192.168.2.51");
		krakenClient.connect();
		krakenClient.communicate();
		
		String answer = krakenClient.getFinalAnswer();
		if(answer.equalsIgnoreCase(KrakenServerMsg.END_SERVER.toString())){
			System.out.println("\nGénération de jeton réussie\nOn se connecte au serveur en SSL !");
			// test authentification auprès du serveur site via le jeton
			String jeton = StringUtils.bytesToString(krakenClient.getSaved_data());
			
			
			msgList = new ArrayList<String>();
			msgList.add(KrakenClientMsg.KRAKEN_SSL_CLIENT.makeCommand());
			//arg1 = ip central, arg2 = ip auth, arg3 = ip site cible, arg4 = ip cam cible
			msgList.add(KrakenClientMsg.ASK_FOR_STREAMING.makeCommand(" "+jeton));
			msgList.add(KrakenClientMsg.END_SSL_CLIENT.makeCommand());
			
			KrakenSSLClient krakenSslClient = new KrakenSSLClient(msgList, "192.168.2.41",0);
			krakenSslClient.connect();
			krakenSslClient.communicate();
			
			String answer_authentification = krakenSslClient.getFinalAnswer();
			if(answer_authentification.equalsIgnoreCase(KrakenServerMsg.END_SSL_SERVER.toString())){
				System.out.println("\nAuthentification réussie\n"+
								"On se connecte au serveur en SSL pour recevoir le streaming!");
				// test authentification auprès du serveur site via le jeton
				String jeton_video = StringUtils.bytesToString(krakenSslClient.getSaved_data());
				
				
				msgList = new ArrayList<String>();
				msgList.add(KrakenClientMsg.KRAKEN_SSL_CLIENT.makeCommand());
				//arg1 = ip central, arg2 = ip auth, arg3 = ip site cible, arg4 = ip cam cible
				msgList.add(KrakenClientMsg.SEND_TOKEN_FOR_VIDEO.makeCommand(" "+jeton_video));
				msgList.add(KrakenClientMsg.END_SSL_CLIENT.makeCommand());
				
				KrakenSSLClient krakenSslClientVideo = new KrakenSSLClient(msgList, "192.168.2.41",1);
				krakenSslClientVideo.connect();
				krakenSslClientVideo.communicate();
			}
			else{
				System.out.println("Authentification error");
			}
		}
		else{
			System.out.println("Génération de jeton failed");
		}
	}

}
